#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <assert.h>
#include <string.h>

#include "iic_test.h"

void fault_record()
{
	unsigned int fd1=open("./file.txt",O_RDWR|O_APPEND|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
	unsigned int fd=open("./temp.txt",O_RDWR);
	char buf[1024];
	int real_read_len=0;
    while((real_read_len=read(fd,buf,sizeof(buf)))>0)
	{
        write(fd1,buf,real_read_len);
	}
	write(fd1,"\r\n",2);
	close(fd1);
	close(fd);
}

void file_write(int fd1,int n,int num_s,int num_f,char* arg)
{
	char string[100]="commend name:";
	write(fd1,string,13);
	
	char cmd[100];
	int i=0;
	for(;i<strlen(arg);i++)
	{
		cmd[i]=*(arg+i);
	}
	cmd[i]='\0';
	write(fd1,cmd,i);
	write(fd1," ",1);
	char name[100]="test time:";
	write(fd1,name,10);
	char str[3];
	str[0]=(n/10)+'0';
	str[1]=(n%10)+'0';
	str[2]=' ';
	write(fd1,str,3);		
	
	char string1[100]="success time:";
	write(fd1,string1,13);
	char str1[3];
	str1[0]=(num_s/10)+'0';
	str1[1]=(num_s%10)+'0';
	str1[2]=' ';
	write(fd1,str1,3);
		
	char string2[100]="fail time:";
	write(fd1,string2,10);
	char str2[3];
	str2[0]=(num_f/10)+'0';
	str2[1]=(num_f%10)+'0';
	str2[2]=' ';
	write(fd1,str2,3);
	write(fd1,"\r\n",2);
	
	/*unsigned int fd=open("./temp.txt",O_RDWR);
	char buf[1024];
	int real_read_len=0;
    while((real_read_len=read(fd,buf,sizeof(buf)))>0)
	{
        write(fd1,buf,real_read_len);
	}
	write(fd1,"\r\n",2);*/
}

bool chos_cmd(char *arg, int n,int fd,int *out_value)
{
	//unsigned int fd = open_i2c_device("/dev/i2c-2");
	unsigned int fd1=open("./file.txt",O_RDWR|O_APPEND|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
	int j=0;
	int i=1;
	if(!(i=strcmp(arg,"SET_BEAM")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
			tests=set_beam(fd);
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		
		file_write(fd1,n,num_s,num_f,arg);
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}
	
	if(!(i=strcmp(arg,"SET_BEAM0")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
			tests=set_beam0(fd);
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		file_write(fd1,n,num_s,num_f,arg);	
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}
	
	if(!(i=strcmp(arg,"SET_BEAM1")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
			tests=set_beam1(fd);
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		file_write(fd1,n,num_s,num_f,arg);	
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}
	
	if(!(i=strcmp(arg,"SET_BEAM2")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
			tests=set_beam2(fd);
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		file_write(fd1,n,num_s,num_f,arg);	
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}
	
	if(!(i=strcmp(arg,"SET_BEAM3")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
			tests=set_beam3(fd);
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		file_write(fd1,n,num_s,num_f,arg);	
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}
	
	if(!(i=strcmp(arg,"RESET")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
			tests=enter_reset(fd);
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		file_write(fd1,n,num_s,num_f,arg);	
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}

	if(!(i=strcmp(arg,"VERSION")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
			tests=query_version(fd);
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		file_write(fd1,n,num_s,num_f,arg);	
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}
	
	if(!(i=strcmp(arg,"READ_ANGLE")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
            tests=read_angle(fd,out_value);
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		file_write(fd1,n,num_s,num_f,arg);	
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}
	
	if(!(i=strcmp(arg,"LOOP")))
	{
		bool tests=0;
		int num_s=0;
		int num_f=0;
		while(j<n)
		{
			bool test_query_version = query_version(fd);
            bool test_read_angle = read_angle(fd,out_value);
			bool test_enter_reset = enter_reset(fd);
			bool test_set_beam = set_beam(fd);
			bool test_set_beam0 = set_beam0(fd);
			bool test_set_beam1 = set_beam1(fd);
			bool test_set_beam2 = set_beam2(fd);
			bool test_set_beam3 = set_beam3(fd);
			
			tests=(test_query_version && test_read_angle && test_set_beam && test_set_beam0 &&
			test_set_beam1 && test_set_beam2 && test_set_beam3);
			
			if(tests)
			{
				num_s++;
			}
			else
			{
				num_f++;
			}
			j++;
		}
		file_write(fd1,n,num_s,num_f,arg);	
		if(num_f>0)
		{
			fault_record();
		}
		return true;
	}
	close(fd1);
	return false;
}
